package com.lordjoe.collectiveintelligence.data;

import java.util.*;

/**
 * com.lordjoe.collectiveintelligence.data.WeightedItemDistanceMetric
 *
 * @author Steve Lewis
 * @date May 12, 2009
 */
public class WeightedItemDistanceMetric  implements IDistanceMetric<IDataItem>
{
    public static WeightedItemDistanceMetric[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = WeightedItemDistanceMetric.class;

    private final Map<IMeasurementType,IDistanceMetric> m_Weights
            = new HashMap<IMeasurementType,IDistanceMetric>();

    public WeightedItemDistanceMetric()
    {
    }

    public void addWeight(IMeasurementType type,IDistanceMetric dm)
    {
         m_Weights.put(type,dm);
    }

    public IDistanceMetric getWeight(IMeasurementType type)
    {
         return m_Weights.get(type);
    }

    public double getDistance(IDataItem t1, IDataItem t2)
    {
        double distance = 0;
        IMeasurementType[] types = t1.getMeasurementTypes();
        for (int i = 0; i < types.length; i++) {
            IMeasurementType type = types[i];
            IDistanceMetric dm = getWeight(type);
            IMeasurement m1 = t1.getMeasurement(type);
            IMeasurement m2 = t2.getMeasurement(type);
            distance += dm.getDistance(m1,m2);
        }
        return distance;
    }
}
